//
//  RCMemberVC.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-19.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMemberVC.h"
#import "UIColor-Expanded.h"
#import "RCMemberInfoDataModel.h"
#import "NSArray+EX.h"
#import "RCMemberTableViewCell.h"

#define kTableViewFrame             CGRectMake(0,0,320,UIScreenHeight-64-49)

#define kSignalImageFile            @"image_member_signal.png"

@interface RCMemberVC ()

@property(nonatomic,retain) UITableView *memberTabelView;
@property(nonatomic,assign) int64_t requestID;
@property(nonatomic,retain) MBProgressHUDEx *progressHub;
@property(nonatomic,retain) RCMemberInfoDataModel *memberDataModel;

@end

@implementation RCMemberVC

@synthesize memberTabelView = _memberTabelView;
@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;
@synthesize memberDataModel = _memberDataModel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"如意行卡";
    self.navigationItem.titleView = [self createTitleView];
    NSArray *viewControllersArray = [self.navigationController viewControllers];
    if (viewControllersArray && [viewControllersArray count] <= 1){
        
        UIButton *signalButton = [UIButton buttonWithType:UIButtonTypeCustom];
        signalButton.frame = CGRectMake(0, 0, 44, 44);
        [signalButton setImage:[UIImage imageNamed:kSignalImageFile]
                      forState:UIControlStateNormal];
        [signalButton setImage:[UIImage imageNamed:kSignalImageFile]
                      forState:UIControlStateHighlighted];
        self.navigationItem.leftBarButtonItem = [self createLeftBarButtonItemWithButton:signalButton];
    }
    
    
}

-(void)dealloc{
    self.memberTabelView = nil;
    self.memberDataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (!_memberTabelView) {
        _memberTabelView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _memberTabelView.backgroundColor = [UIColor colorWithHex:0xfff6f6f6];
        _memberTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _memberTabelView.delegate = self;
        _memberTabelView.dataSource = self;
    }
    [self.viewContainer addSubview:_memberTabelView];
    
    NSArray *viewControllersArray = [self.navigationController viewControllers];
    if (viewControllersArray && [viewControllersArray count] > 1){
        _memberTabelView.frame = CGRectMake(0,0,320,UIScreenHeight-64);
        
    }
    [self requestData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

-(void)requestData{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestMembersInfo:self];
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        self.memberDataModel = (RCMemberInfoDataModel *)dataModel;
        [_memberTabelView reloadData];
    }
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (nil == self.memberDataModel || nil == self.memberDataModel.detailInfoArray || [self.memberDataModel.detailInfoArray count] == 0) {
        return 0;
    }
    return [self.memberDataModel.detailInfoArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (nil == self.memberDataModel || nil == self.memberDataModel.detailInfoArray || [self.memberDataModel.detailInfoArray count] == 0) {
        return 0;
    }
    RCMemberDetailInfo *detailInfo = [self.memberDataModel.detailInfoArray safeObjectAtIndex:[indexPath row]];
    return [RCMemberTableViewCell getCellHeight:detailInfo];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"memberTabelViewCell";
    RCMemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCMemberTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
    }
    NSInteger row = [indexPath row];
    RCMemberDetailInfo *detailInfo = [self.memberDataModel.detailInfoArray safeObjectAtIndex:row];
    BOOL lastCell = NO;
    if (row == [self.memberDataModel.detailInfoArray count] -1) {
        lastCell = YES;
    }
    [cell setContentViewWithData:detailInfo CellIndex:row isLastCell:lastCell];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
